﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Module_Ribbon_Bar_Detail : DetailPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initializeWebForm();

        if (!IsPostBack)
        {
            Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

            System.Data.DataTable ribbonTable = moduleProvider.RibbonGetList();

            ribbonDropDownList.DataSource = ribbonTable;
            ribbonDropDownList.DataTextField = "RibbonName";
            ribbonDropDownList.DataValueField = "RibbonGuid";

            ribbonDropDownList.DataBind();

            Guid _ribbonBarGuid = Guid.Empty;

            if (Request.QueryString["RibbonBarGuid"] != null)
            {
                _ribbonBarGuid = new Guid(Request.QueryString["RibbonBarGuid"]);
            }

            if (_ribbonBarGuid != Guid.Empty)
            {
                System.Data.DataTable ribbonBarTable = moduleProvider.RibbonBarGetByRibbonBarGuid(_ribbonBarGuid);

                if (ribbonBarTable.Rows.Count == 1)
                {
                    System.Data.DataRow ribbonBarRow = ribbonBarTable.Rows[0];

                    ribbonBarIdTextBox.Text = System.Convert.ToString(ribbonBarRow["RibbonBarId"]);
                    ribbonBarGuidTextBox.Text = System.Convert.ToString(ribbonBarRow["RibbonBarGuid"]);
                    orderTextBox.Text = System.Convert.ToString(ribbonBarRow["RibbonBarOrder"]);

                    ribbonBarNameTextBox.Text = System.Convert.ToString(ribbonBarRow["RibbonBarName"]);

                    string _ribbonGuidString = System.Convert.ToString(ribbonBarRow["RibbonGuid"]);

                    foreach (ListItem ribbonItem in ribbonDropDownList.Items)
                    {
                        if (ribbonItem.Value == _ribbonGuidString)
                        {
                            ribbonItem.Selected = true;
                            break;
                        }
                    }
                }
            }

            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            System.Data.DataTable roleTable = securityProvider.RoleGetList();

            groupDropDownList.DataSource = roleTable;
            groupDropDownList.DataTextField = "RoleName";
            groupDropDownList.DataValueField = "RoleGuid";

            groupDropDownList.DataBind();

            System.Data.DataTable userRoleTable = securityProvider.RoleGetByRibbonBarGuid(_ribbonBarGuid);

            groupListBox.DataSource = userRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            switch (formMode)
            {
                case FormMode.Create:
                    ribbonBarNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    ribbonBarNameTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Delete:
                    ribbonBarNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    ribbonBarNameTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;

                    deleteImageButton.Visible = true;
                    break;
                case FormMode.Edit:
                    ribbonBarNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    ribbonBarNameTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Default:
                    ribbonBarNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    ribbonBarNameTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;

                    deleteImageButton.Visible = false;
                    break;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(this.buildForwardUrl("Default.aspx"));
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

        if (formMode == FormMode.Edit)
        {
            if (!moduleProvider.RibbonBarUpdate(
                System.Convert.ToInt32(ribbonBarIdTextBox.Text),
                new Guid(ribbonBarGuidTextBox.Text),
                new Guid(ribbonDropDownList.SelectedValue),
                ribbonBarNameTextBox.Text,
                System.Convert.ToInt32(orderTextBox.Text)))
            {
                Response.Write("alert(\"An error occured while updating the database\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
        else if (formMode == FormMode.Create)
        {
            if (moduleProvider.RibbonBarInsert(
                Guid.NewGuid(),
                new Guid(ribbonDropDownList.SelectedValue),
                ribbonBarNameTextBox.Text,
                System.Convert.ToInt32(orderTextBox.Text)) == 0)
            {
                Response.Write("alert(\"An error occured while creating database entry\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
    }
    protected void groupListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        UnsubscribeButton.Enabled = groupListBox.SelectedIndex != -1;
    }
    protected void subscribeButton_Click(object sender, EventArgs e)
    {
        Guid _ribbonBarGuid = Guid.Empty;

        if (Request.QueryString["RibbonBarGuid"] != null)
        {
            _ribbonBarGuid = new Guid(Request.QueryString["RibbonBarGuid"]);
        }

        if (_ribbonBarGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.RibbonBarRoleInsert(_ribbonBarGuid, new Guid(groupDropDownList.SelectedValue));

            System.Data.DataTable ribbonBarRoleTable = securityProvider.RoleGetByRibbonBarGuid(_ribbonBarGuid);

            groupListBox.DataSource = ribbonBarRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();
        }
    }
    protected void UnsubscribeButton_Click(object sender, EventArgs e)
    {
        Guid _ribbonBarGuid = Guid.Empty;

        if (Request.QueryString["RibbonBarGuid"] != null)
        {
            _ribbonBarGuid = new Guid(Request.QueryString["RibbonBarGuid"]);
        }

        if (_ribbonBarGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.RibbonBarRoleDelete(_ribbonBarGuid, new Guid(groupListBox.SelectedValue));

            System.Data.DataTable ribbonBarRoleTable = securityProvider.RoleGetByRibbonBarGuid(_ribbonBarGuid);

            groupListBox.DataSource = ribbonBarRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            UnsubscribeButton.Enabled = false;
        }
    }
}
